www.gusucode.com > VC++ 本地IE网址收藏夹管理器源码程序 > VC++ 本地IE网址收藏夹管理器源码程序\code\FavoritesTryDlg.cpp

    // FavoritesTryDlg.cpp : implementation file
// GDGF 编写于2001年11月13日
////////////////////////////////////////////
// Download by http://www.NewXing.com
#include "stdafx.h"
#include "FavoritesTry.h"
#include "FavoritesTryDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

LPBYTE favpath = new BYTE[80];
CString StrPath;
CString url;

CFileFind finder;
BOOL bWorking;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFavoritesTryDlg dialog

CFavoritesTryDlg::CFavoritesTryDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFavoritesTryDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFavoritesTryDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFavoritesTryDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFavoritesTryDlg)
	DDX_Control(pDX, IDC_EDIT_URL, m_EDIT_URL);
	DDX_Control(pDX, IDC_TREE_FAV, m_TREE_FAV);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFavoritesTryDlg, CDialog)
//{{AFX_MSG_MAP(CFavoritesTryDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(TVN_SELCHANGING, IDC_TREE_FAV, OnSelchangingTreeFav)
ON_NOTIFY(NM_DBLCLK, IDC_TREE_FAV, OnDblclkTreeFav)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_FAV, OnSelchangedTreeFav)
	ON_BN_CLICKED(IDC_GOURL, OnGourl)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFavoritesTryDlg message handlers

BOOL CFavoritesTryDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	GetDlgItem(IDC_GOURL)->ShowWindow(false);
	// 设置树控件图标
	CImageList* pInfoImages = new CImageList;
    pInfoImages->Create(16, 16, ILC_COLOR8 | ILC_MASK, 2, 0);
    pInfoImages->Add(AfxGetApp()->LoadIcon(IDI_ICON1));
    pInfoImages->Add(AfxGetApp()->LoadIcon(IDI_ICON2));
    m_TREE_FAV.SetImageList(pInfoImages, TVSIL_NORMAL);
	
	// 从注册表获取收藏夹所在位置
	HKEY hKEY;
	DWORD type=REG_SZ;
	LPCTSTR path="Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
	DWORD cbData=80;
	::RegOpenKeyEx(HKEY_CURRENT_USER,path,0,KEY_READ,&hKEY);
	::RegQueryValueEx(hKEY,"Favorites",NULL,&type,favpath,&cbData);
	::RegCloseKey(hKEY);

	OnLoad();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFavoritesTryDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFavoritesTryDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}	
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFavoritesTryDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CFavoritesTryDlg::OnLoad() 
{
	StrPath = (CString)favpath + "\\*.*";
	bWorking = finder.FindFile(StrPath);  
	while(bWorking)
	{	  
		bWorking = finder.FindNextFile();
        if (!finder.IsDots() && finder.IsDirectory()) 
		{
			HandleDirectory();
		}
	}

	StrPath = (CString)favpath + "\\*.url"; 
	HandleFile(StrPath);
	
	finder.Close();
}

void CFavoritesTryDlg::HandleDirectory()
{
    HTREEITEM hTreeItem = m_TREE_FAV.InsertItem(finder.GetFileName(), 0, 1, TVI_ROOT, TVI_LAST);

	CString msg;
	msg = (CString)favpath + "\\" + finder.GetFileName() + "\\*.url";

	CFileFind finder2;
    BOOL bWorking2 = finder2.FindFile(msg);
	while(bWorking2)
	{	
	    bWorking2 = finder2.FindNextFile();
		m_TREE_FAV.InsertItem(finder2.GetFileName(), 0, 1, hTreeItem, TVI_LAST);
	}
}

void CFavoritesTryDlg::HandleFile(CString FilePath)
{
	bWorking = finder.FindFile(FilePath);
	while(bWorking)
	{	  
		bWorking = finder.FindNextFile();
		   m_TREE_FAV.InsertItem(finder.GetFileName(), 0, 1, TVI_ROOT, TVI_LAST);
	}
}

void CFavoritesTryDlg::OnSelchangingTreeFav(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	
	HTREEITEM hTreeItem = m_TREE_FAV.GetSelectedItem();
    CString msg = m_TREE_FAV.GetItemText(hTreeItem);

	HTREEITEM hParent = m_TREE_FAV.GetParentItem(hTreeItem);
    if(hParent != NULL)
	{
        StrPath = (CString)favpath + "\\" + 
			       m_TREE_FAV.GetItemText(hParent) + "\\" + msg;
	}
    else
	    StrPath = (CString)favpath + "\\" + msg;
	
    FILE* file;
	if(file = fopen(StrPath,"rb"))
	{
		int seek = 1;
		char check[1];
		check[0]='\0';
		check[1]='\0';
		while(check[0] != '=')
		{
			seek++;
			fseek(file,seek,0);  	
			fread(check,1,1,file);
		}
		seek = ftell(file);
		fclose(file);
		
		CStdioFile stdiofile; 
		stdiofile.Open(StrPath,CFile::modeRead);
		stdiofile.Seek(seek ,0);
		stdiofile.ReadString(url);
		m_EDIT_URL.SetSel(0,-1);
    	m_EDIT_URL.ReplaceSel(url);		
	}
	*pResult = 0;
}

void CFavoritesTryDlg::OnSelchangedTreeFav(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	
	HTREEITEM hCurrent = m_TREE_FAV.GetSelectedItem();
	CString msg = m_TREE_FAV.GetItemText(hCurrent);

	HTREEITEM hParent = m_TREE_FAV.GetParentItem(hCurrent);
    if(hParent != NULL)
	{
        StrPath = (CString)favpath + "\\" + 
			       m_TREE_FAV.GetItemText(hParent) + "\\" + msg;
	}
    else
	    StrPath = (CString)favpath + "\\" + msg;
	
    FILE* file;
	if(file = fopen(StrPath,"rb"))
	{
		int seek = 1;
		char check[1];
		check[0]='\0';
		check[1]='\0';
		while(check[0] != '=')
		{
			seek++;
			fseek(file,seek,0);  	
			fread(check,1,1,file);
		}
		seek = ftell(file);
		fclose(file);
		
		CStdioFile stdiofile; 
		stdiofile.Open(StrPath,CFile::modeRead);
		stdiofile.Seek(seek ,0);
		stdiofile.ReadString(url);
		m_EDIT_URL.SetSel(0,-1);
    	m_EDIT_URL.ReplaceSel(url);		
	}	
	*pResult = 0;
}

BOOL CFavoritesTryDlg::PreTranslateMessage(MSG* pMsg) 
{
    if(pMsg -> message == WM_KEYDOWN)
	{
        if(pMsg -> wParam == VK_ESCAPE)
			return TRUE;
	}	
	return CDialog::PreTranslateMessage(pMsg);
}

void CFavoritesTryDlg::OnDblclkTreeFav(NMHDR* pNMHDR, LRESULT* pResult) 
{
	OnGourl();

	*pResult = 0;
}

void CFavoritesTryDlg::OnGourl() 
{
	HTREEITEM hSel=m_TREE_FAV.GetSelectedItem();
	if(!m_TREE_FAV.ItemHasChildren(hSel))  // 没有子树
	{		
		HKEY hKEY;
		DWORD type=REG_SZ;
		DWORD cbData=80;
		LPBYTE IEpath = new BYTE[80];
		::RegOpenKeyEx(HKEY_CLASSES_ROOT,"htmlfile\\shell\\open\\command",0,KEY_READ,&hKEY);
		::RegQueryValueEx(hKEY,NULL,NULL,&type,IEpath,&cbData);
		::RegCloseKey(hKEY);
		char ShellChar[256]; 
		strcpy(ShellChar,(char *)IEpath);
		strcat(ShellChar,url);
		WinExec(ShellChar,SW_SHOW);
	}		
}